package com.czy.owner.ui.wheel;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import com.czy.owner.R;
import com.czy.owner.adapter.WheelFilterChildAdapter;
import com.czy.owner.entity.WheelAttrModel;
import com.czy.owner.utils.MyLog;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Raven on 2018/1/10.
 */

public class WheelFilterChildMenuLayout extends FrameLayout implements View.OnClickListener{

    private RecyclerView mClassifyList;
    private Button mButtonOK;
    private Button mButtonCancel;

    private Context mContext;
    private WheelFilterChildAdapter wheelFilterChildAdapter;
    List<WheelAttrModel> allBrandList;
    List<WheelAttrModel> copyBrandList;//用于展示

    public WheelFilterChildMenuLayout(@NonNull Context context,List<WheelAttrModel> allBrandList,List<WheelAttrModel> copyBrandList) {
        super(context);
        this.mContext = context;
        this.allBrandList = allBrandList;
        this.copyBrandList = copyBrandList;
        MyLog.d("RAVEN","===WheelFilterChildMenuLayout===");
        //已选中的状态带过来
        for(int i = 0 ; i < allBrandList.size();++i){
            for(int j = 0 ; j < copyBrandList.size();++j){
                if (allBrandList.get(i).getId() == copyBrandList.get(j).getId()){
                    copyBrandList.get(j).setChoosed(allBrandList.get(i).isChoosed());
                }
            }
        }
        initView();
    }

    //初始化view
    private void initView() {
        View.inflate(getContext(), R.layout.layout_wheel_filter_child_side, this);
        mClassifyList = (RecyclerView) findViewById(R.id.select_classify_list);
        mButtonOK = (Button) findViewById(R.id.btn_ok);
        mButtonCancel = (Button) findViewById(R.id.btn_cancel);
        mButtonOK.setOnClickListener(this);
        mButtonCancel.setOnClickListener(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mClassifyList.setLayoutManager(linearLayoutManager);
        wheelFilterChildAdapter = new WheelFilterChildAdapter(mContext);
        mClassifyList.setAdapter(wheelFilterChildAdapter);
        wheelFilterChildAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, Object o) {
                MyLog.d("RAVEN","wheelFilterChildAdapter onItemClicked " + position);
                wheelFilterChildAdapter.getAllData().get(position).setChoosed(!wheelFilterChildAdapter.getAllData().get(position).isChoosed());
                wheelFilterChildAdapter.notifyDataSetChanged();
            }
        });
        wheelFilterChildAdapter.addAll(copyBrandList);


    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_ok:
                if(onBtnClickedListener != null){
                    onBtnClickedListener.onEnsure(wheelFilterChildAdapter.getAllData());
                }
                break;
            case R.id.btn_cancel:
                if(onBtnClickedListener != null){
                    onBtnClickedListener.onCancel();
                }
                break;
        }
    }

    private OnBtnClickedListener onBtnClickedListener;
    public interface OnBtnClickedListener{
        void onCancel();
        void onEnsure(List<WheelAttrModel> optAttrs);
    }
    public void setOnBtnClickedListener(OnBtnClickedListener listener){
        this.onBtnClickedListener = listener;
    }
}
